#coding=utf-8
#Boa:Dialog:DlgPeerEditor

import wx

from product import CDataPeer
from config import CBriefCode

def create(parent):
    return DlgPeerEditor(parent)

[wxID_DLGPEEREDITOR, wxID_DLGPEEREDITORBTNCANCEL, wxID_DLGPEEREDITORBTNSAVE, 
 wxID_DLGPEEREDITORTADDR, wxID_DLGPEEREDITORTBRIEFCODE, 
 wxID_DLGPEEREDITORTCADDR, wxID_DLGPEEREDITORTCBRIEFCODE, 
 wxID_DLGPEEREDITORTCDEBT, wxID_DLGPEEREDITORTCEMAIL, wxID_DLGPEEREDITORTCFAX, 
 wxID_DLGPEEREDITORTCLINKMAN, wxID_DLGPEEREDITORTCLMPHONE, 
 wxID_DLGPEEREDITORTCMEMO, wxID_DLGPEEREDITORTCNAME, 
 wxID_DLGPEEREDITORTCPHONE, wxID_DLGPEEREDITORTDEBT, wxID_DLGPEEREDITORTEMAIL, 
 wxID_DLGPEEREDITORTFAX, wxID_DLGPEEREDITORTLINKMAN, 
 wxID_DLGPEEREDITORTLMPHONE, wxID_DLGPEEREDITORTMEMO, wxID_DLGPEEREDITORTNAME, 
 wxID_DLGPEEREDITORTPHONE, 
] = [wx.NewId() for _init_ctrls in range(23)]

class DlgPeerEditor(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGPEEREDITOR, name=u'DlgPeerEditor',
              parent=prnt, pos=wx.Point(526, 301), size=wx.Size(465, 437),
              style=wx.DEFAULT_DIALOG_STYLE, title='Dialog1')
        self.SetClientSize(wx.Size(457, 403))
        self.Center(wx.BOTH)

        self.TName = wx.StaticText(id=wxID_DLGPEEREDITORTNAME,
              label=u'\u5355\u4f4d\u540d\u79f0\uff1a', name=u'TName',
              parent=self, pos=wx.Point(32, 48), size=wx.Size(60, 14), style=0)

        self.TCName = wx.TextCtrl(id=wxID_DLGPEEREDITORTCNAME, name=u'TCName',
              parent=self, pos=wx.Point(104, 40), size=wx.Size(100, 22),
              style=0, value=u'')
        self.TCName.Bind(wx.EVT_TEXT, self.OnTCNameText,
              id=wxID_DLGPEEREDITORTCNAME)

        self.TBriefCode = wx.StaticText(id=wxID_DLGPEEREDITORTBRIEFCODE,
              label=u'\u52a9\u8bb0\u7801\uff1a', name=u'TBriefCode',
              parent=self, pos=wx.Point(240, 48), size=wx.Size(48, 14),
              style=0)

        self.TCBriefCode = wx.TextCtrl(id=wxID_DLGPEEREDITORTCBRIEFCODE,
              name=u'TCBriefCode', parent=self, pos=wx.Point(312, 40),
              size=wx.Size(100, 22), style=0, value=u'')

        self.TCEmail = wx.TextCtrl(id=wxID_DLGPEEREDITORTCEMAIL,
              name=u'TCEmail', parent=self, pos=wx.Point(312, 80),
              size=wx.Size(100, 22), style=0, value=u'')
        self.TCEmail.SetAutoLayout(False)

        self.TEmail = wx.StaticText(id=wxID_DLGPEEREDITORTEMAIL,
              label=u'\u7535\u5b50\u90ae\u4ef6\uff1a', name=u'TEmail',
              parent=self, pos=wx.Point(240, 88), size=wx.Size(60, 14),
              style=0)

        self.TCPhone = wx.TextCtrl(id=wxID_DLGPEEREDITORTCPHONE,
              name=u'TCPhone', parent=self, pos=wx.Point(104, 80),
              size=wx.Size(100, 22), style=0, value=u'')

        self.TPhone = wx.StaticText(id=wxID_DLGPEEREDITORTPHONE,
              label=u'\u516c\u53f8\u7535\u8bdd\uff1a', name=u'TPhone',
              parent=self, pos=wx.Point(32, 88), size=wx.Size(60, 14), style=0)

        self.TAddr = wx.StaticText(id=wxID_DLGPEEREDITORTADDR,
              label=u'\u8054\u7cfb\u5730\u5740\uff1a', name=u'TAddr',
              parent=self, pos=wx.Point(32, 128), size=wx.Size(60, 14),
              style=0)

        self.TCAddr = wx.TextCtrl(id=wxID_DLGPEEREDITORTCADDR, name=u'TCAddr',
              parent=self, pos=wx.Point(104, 120), size=wx.Size(100, 22),
              style=0, value=u'')

        self.TFax = wx.StaticText(id=wxID_DLGPEEREDITORTFAX,
              label=u'\u4f20      \u771f\uff1a', name=u'TFax', parent=self,
              pos=wx.Point(240, 128), size=wx.Size(60, 14), style=0)

        self.TCFax = wx.TextCtrl(id=wxID_DLGPEEREDITORTCFAX, name=u'TCFax',
              parent=self, pos=wx.Point(312, 120), size=wx.Size(100, 22),
              style=0, value=u'')

        self.TCLmPhone = wx.TextCtrl(id=wxID_DLGPEEREDITORTCLMPHONE,
              name=u'TCLmPhone', parent=self, pos=wx.Point(312, 160),
              size=wx.Size(100, 22), style=0, value=u'')

        self.TLmPhone = wx.StaticText(id=wxID_DLGPEEREDITORTLMPHONE,
              label=u'\u8054\u7cfb\u4eba\u7535\u8bdd\uff1a', name=u'TLmPhone',
              parent=self, pos=wx.Point(240, 168), size=wx.Size(72, 14),
              style=0)

        self.TCLinkman = wx.TextCtrl(id=wxID_DLGPEEREDITORTCLINKMAN,
              name=u'TCLinkman', parent=self, pos=wx.Point(104, 160),
              size=wx.Size(100, 22), style=0, value=u'')

        self.TLinkman = wx.StaticText(id=wxID_DLGPEEREDITORTLINKMAN,
              label=u'\u8054\u7cfb\u4eba\uff1a', name=u'TLinkman', parent=self,
              pos=wx.Point(32, 168), size=wx.Size(48, 14), style=0)

        self.TMemo = wx.StaticText(id=wxID_DLGPEEREDITORTMEMO,
              label=u'\u5907      \u6ce8\uff1a', name=u'TMemo', parent=self,
              pos=wx.Point(32, 200), size=wx.Size(60, 14), style=0)

        self.TCMemo = wx.TextCtrl(id=wxID_DLGPEEREDITORTCMEMO, name=u'TCMemo',
              parent=self, pos=wx.Point(104, 208), size=wx.Size(288, 56),
              style=wx.TE_MULTILINE, value=u'')

        self.TCDebt = wx.TextCtrl(id=wxID_DLGPEEREDITORTCDEBT, name=u'TCDebt',
              parent=self, pos=wx.Point(104, 280), size=wx.Size(100, 22),
              style=0, value=u'0')

        self.TDebt = wx.StaticText(id=wxID_DLGPEEREDITORTDEBT,
              label=u'\u671f\u521d\u5e94\u6536\uff1a', name=u'TDebt',
              parent=self, pos=wx.Point(32, 280), size=wx.Size(60, 14),
              style=0)

        self.btnSave = wx.Button(id=wxID_DLGPEEREDITORBTNSAVE,
              label=u'\u4fdd\u5b58', name=u'btnSave', parent=self,
              pos=wx.Point(88, 336), size=wx.Size(75, 24), style=0)
        self.btnSave.SetDefault()
        self.btnSave.Bind(wx.EVT_BUTTON, self.OnBtnSaveButton,
              id=wxID_DLGPEEREDITORBTNSAVE)

        self.btnCancel = wx.Button(id=wxID_DLGPEEREDITORBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(248, 336), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGPEEREDITORBTNCANCEL)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.myInit()
        
    def myInit(self):
        self.m_peer = None

    def setEditObj(self, obj):
        self.m_peer = obj
        
        self.TCName.SetValue(obj.getValue(obj.fi_name))
        self.TCBriefCode.SetValue(obj.getValue(obj.fi_briefCode))
        self.TCPhone.SetValue(obj.getValue(obj.fi_phone))
        self.TCEmail.SetValue(obj.getValue(obj.fi_email))
        self.TCAddr.SetValue(obj.getValue(obj.fi_addr))
        self.TCFax.SetValue(obj.getValue(obj.fi_fax))
        self.TCLinkman.SetValue(obj.getValue(obj.fi_linkman))
        self.TCLmPhone.SetValue(obj.getValue(obj.fi_lmPhone))
        self.TCMemo.SetValue(obj.getValue(obj.fi_memo))
        self.TCDebt.SetValue(str(obj.getValue(obj.fi_debt)))
        
    def OnTCNameText(self, event):
        name = self.TCName.GetValue()
        code = CBriefCode.getBriefCode(name)
        self.TCBriefCode.SetValue(code)

    def OnBtnSaveButton(self, event):
        if (self.TCName.GetValue() == ''):
            dlg = wx.MessageDialog(self, u'必须设置单位姓名', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return

        cls = CDataPeer
        data = {cls.fi_name:self.TCName.GetValue(),
                cls.fi_briefCode:self.TCBriefCode.GetValue(),
                cls.fi_phone:self.TCPhone.GetValue(),
                cls.fi_addr:self.TCAddr.GetValue(),
                cls.fi_email:self.TCEmail.GetValue(),
                cls.fi_fax:self.TCFax.GetValue(),
                cls.fi_memo:self.TCMemo.GetValue(),
                cls.fi_linkman:self.TCLinkman.GetValue(),
                cls.fi_lmPhone:self.TCLmPhone.GetValue(),
                cls.fi_debt:float(self.TCDebt.GetValue())}
        
        if (self.m_peer is not None):
            self.m_peer.setMulFieldVal(data)
            self.m_peer.update()
        else:
            obj = cls.factory()
            #obj.assignNewId()
            obj.setMulFieldVal(data)
            obj.insert()
            self.m_peer = obj
            
        #self.SetReturnCode(wx.ID_OK)
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)